import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

@WebServlet(name = "ServletDemo1", urlPatterns = "/ServletDemo1")
public class dd extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String consName = req.getParameter("like");
        String type = req.getParameter("time");
        String encodeStr = URLEncoder.encode(consName, "utf-8");//转换为%E7%99%BD%E7%BE%8A%E5%BA%A7这种格式
        String httpurl = "http://web.juhe.cn/constellation/getAll";//接口名
        String params = "consName=" + encodeStr + "&type=" + type + "&key=34bd578313b03d4a589686d9dfda707d";//如此拼接即可
        String result = Utils(httpurl, params);
        JSONObject jsonObject = JSONObject.fromObject(result);
        //实体类
        Constellation constellation = new Constellation(
                jsonObject.getString("date"),
                jsonObject.getString("name"),
                jsonObject.getString("QFriend"),
                jsonObject.getString("color"),
                jsonObject.getString("datetime"),
                jsonObject.getString("health"),
                jsonObject.getString("love"),
                jsonObject.getString("work"),
                jsonObject.getString("money"),
                jsonObject.getString("number"),
                jsonObject.getString("summary"),
                jsonObject.getString("all")
        );

        req.setAttribute("Constellation", constellation);
        req.setAttribute("msg", 22);
        req.getRequestDispatcher("WEB-INF/show.jsp").forward(req, resp);
    }

    public static String Utils(String url, String params) {
        PrintWriter out = null;
        BufferedReader in = null;
        StringBuilder result = new StringBuilder();
        try {
            URL reqUrl = new URL(url);
            // 建立连接
            URLConnection conn = reqUrl.openConnection();

            //设置请求头
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
            //          conn.setRequestProperty("Connection", "Keep-Alive");//保持长连接
            conn.setDoOutput(true); //设置为true才可以使用conn.getOutputStream().write()
            conn.setDoInput(true); //才可以使用conn.getInputStream().read();

            //写入参数
            out = new PrintWriter(conn.getOutputStream());
            //设置参数，可以直接写&参数，也可以直接传入拼接好的
            out.print(params);
            // flush输出流的缓冲
            out.flush();

            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {// 使用finally块来关闭输出流、输入流
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return result.toString();
    }

}
